﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.work3
{
    abstract class Shape
    {
        public abstract bool IsValid { get; }
        public abstract double GetArea();
    }

    // 长方形
    class Rectangle : Shape
    {
        public double Length { get; }
        public double Width { get; }

        public Rectangle(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public override bool IsValid => Length > 0 && Width > 0;
        public override double GetArea() => IsValid ? Length * Width : 0;
    }


    class Square : Shape
    {
        public double Side { get; }

        public Square(double side)
        {
            Side = side;
        }

        public override bool IsValid => Side > 0;
        public override double GetArea() => IsValid ? Side * Side : 0;
    }

    class Triangle : Shape
    {
        public double A { get; }
        public double B { get; }
        public double C { get; }

        public Triangle(double a, double b, double c)
        {
            A = a;
            B = b;
            C = c;
        }

        public override bool IsValid =>
            A > 0 && B > 0 && C > 0 &&
            A + B > C && A + C > B && B + C > A;

        public override double GetArea()
        {
            if (!IsValid) return 0;
            double s = (A + B + C) / 2;
            return Math.Sqrt(s * (s - A) * (s - B) * (s - C));  
        }
    }

    // 简单工厂类
    static class ShapeFactory
    {
        private static readonly Random _rand = new Random();

        /// <summary>
        /// 随机创建一个 Shape 对象（Rectangle/Square/Triangle）
        /// </summary>
        public static Shape CreateRandomShape()
        {
            // 随机选 0=Rectangle, 1=Square, 2=Triangle
            int type = _rand.Next(0, 3);

            switch (type)
            {
                case 0:

                    double len = _rand.NextDouble() * 9 + 1;
                    double wid = _rand.NextDouble() * 9 + 1;
                    return new Rectangle(len, wid);

                case 1:

                    double side = _rand.NextDouble() * 9 + 1;
                    return new Square(side);

                case 2:

                    while (true)
                    {
                        double a = _rand.NextDouble() * 9 + 1;
                        double b = _rand.NextDouble() * 9 + 1;
                        // 为保证易成三角形，我们让第三边在 |a-b|~(a+b) 之间
                        double min = Math.Abs(a - b) + 0.001;
                        double max = a + b - 0.001;
                        double c = _rand.NextDouble() * (max - min) + min;
                        var tri = new Triangle(a, b, c);
                        if (tri.IsValid) return tri;
                    }

                default:
                    throw new InvalidOperationException("未知的形状类型");
            }
        }
    }
    class Program
    {
        static void Main()
        {
            const int count = 10;
            List<Shape> shapes = new List<Shape>(count);

         
            for (int i = 0; i < count; i++)
            {
                shapes.Add(ShapeFactory.CreateRandomShape());
            }


            double totalArea = 0;
            for (int i = 0; i < shapes.Count; i++)
            {
                Shape s = shapes[i];
                string info = s switch
                {
                    Rectangle r => $"Rectangle(长={r.Length:F2}, 宽={r.Width:F2})",
                    Square q => $"Square(边={q.Side:F2})",
                    Triangle t => $"Triangle(a={t.A:F2}, b={t.B:F2}, c={t.C:F2})",
                    _ => "Unknown"
                };

                double area = s.GetArea();
                Console.WriteLine($"第{i + 1}个：{info}  → 合法={s.IsValid}，面积={area:F2}");
                totalArea += area;
            }

            Console.WriteLine($"\n10 个形状的面积之和 = {totalArea:F2}");
        }
    }
}
